package com.heima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.reggie.common.R;
import com.heima.reggie.domain.ShoppingCart;
import com.heima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        List<ShoppingCart> list = shoppingCartService.list();
        return R.success(list);
    }

    @PostMapping("/add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart , HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        log.info("---------------------------------------------");
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCart.setUserId(userId);
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart :: getDishId , dishId);
        ShoppingCart getOne = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
        if(getOne != null){
            Long id = getOne.getId();
            LambdaUpdateWrapper<ShoppingCart> shoppingCartLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            shoppingCartLambdaUpdateWrapper.set(ShoppingCart :: getNumber , getOne.getNumber()+1)
                    .eq(ShoppingCart :: getId , id);
            shoppingCartService.update(shoppingCartLambdaUpdateWrapper);
            return R.success("添加成功");
        }
        shoppingCartService.save(shoppingCart);
        return R.success("添加成功");

    }

    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart , HttpSession session){

        log.info("---------------------------------------------");
        Long dishId = shoppingCart.getDishId();
        if(dishId == null){
            Long setmealId = shoppingCart.getSetmealId();
            LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart :: getSetmealId , setmealId);
            ShoppingCart getOne = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
            if(getOne.getNumber() > 1){
                Long id = getOne.getId();
                LambdaUpdateWrapper<ShoppingCart> shoppingCartLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                shoppingCartLambdaUpdateWrapper.set(ShoppingCart :: getNumber , getOne.getNumber()-1)
                        .eq(ShoppingCart :: getId , id);
                shoppingCartService.update(shoppingCartLambdaUpdateWrapper);
                return R.success("删除成功");
            }
            shoppingCartService.removeById(getOne.getId());
            return R.success("删除成功");
        }
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart :: getDishId , dishId);
        ShoppingCart getOne = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
        if(getOne.getNumber() > 1){
            Long id = getOne.getId();
            LambdaUpdateWrapper<ShoppingCart> shoppingCartLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            shoppingCartLambdaUpdateWrapper.set(ShoppingCart :: getNumber , getOne.getNumber()-1)
                    .eq(ShoppingCart :: getId , id);
            shoppingCartService.update(shoppingCartLambdaUpdateWrapper);
            return R.success("删除成功");
        }
        shoppingCartService.removeById(getOne.getId());
        return R.success("删除成功");
    }

    @DeleteMapping("/clean")
    public R<String> delete(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        shoppingCartService.cleanShoppingCart(userId);
        return R.success("删除成功");
    }

}
